﻿using System;
using CoreTypes;
using System.Text;
using System.Data;
using System.Linq;
using WODXMPPCOMLib;
using System.Custom;
using System.Drawing;
using System.Windows.Forms;
using System.Custom.Window;
using System.Custom.Drawing;
using System.ComponentModel;
using System.Collections.Generic;
using XmppServiceTransport.Properties;

namespace XmppServiceTransport
{
   public partial class XmppDriverSettings : Window
   {
      bool FAssigningConfig;
      XmppServiceConfig FSelectedConfig;
      CommunicationServiceObject FServiceDriver;

      public XmppDriverSettings(CommunicationServiceObject driver)
      {
         InitializeComponent();

         FServiceDriver = driver;

         comboBoxSecurity.Items.AddRange(Enum.GetValues(typeof(SecurityEnum)).Cast<Object>().ToArray());
         comboBoxAuth.Items.AddRange(Enum.GetValues(typeof(AuthenticationsEnum)).Cast<Object>().ToArray());

         comboBoxService.Items.Clear();
         comboBoxService.Items.AddRange(FServiceDriver.Services);

         Icon = (Icon)ResourceWrapper.GetObject("Gear");
      }

      internal void Show(string configName)
      {
         comboBoxConfig.Items.Clear();

         List<ServiceConfig> configurations = FServiceDriver.Configurations;

         if (configurations.Count() > 0) comboBoxConfig.Items.AddRange(configurations.ToArray());

         SelectedConfig = FServiceDriver.GetConfig(configName);

         Visualize();
      }
      internal XmppServiceConfig SelectedConfig
      {
         get
         {
            return FSelectedConfig;
         }
         set
         {
            FAssigningConfig = true;
            
            try
            {
               if (value == null || value == FServiceDriver.DefaultConfig)
               {
                  buttonCreate_Click(this, null);
               }
               else
               {
                  FSelectedConfig = value;

                  comboBoxConfig.Text = FSelectedConfig.ToString();

                  textBoxHost.Text = FSelectedConfig.Host;
                  textBoxPort.Text = FSelectedConfig.Port.ToString();
                  checkBoxGateway.Checked = FSelectedConfig.ViaGateway;
                  comboBoxService.SelectedItem = FSelectedConfig.Service;

                  textBoxResource.Text = FSelectedConfig.Resource;
                  numericPriority.Value = FSelectedConfig.Priority;
                  checkBoxResource.Checked = FSelectedConfig.Resource == XmppServiceConfig.DefaultResourceName;

                  comboBoxSecurity.SelectedItem = FSelectedConfig.Security;
                  comboBoxAuth.SelectedItem = FSelectedConfig.Authentication;

                  textBoxHost.ReadOnly = FSelectedConfig.ReadOnly;
                  textBoxPort.ReadOnly = FSelectedConfig.ReadOnly;
                  buttonDelete.Enabled = !FSelectedConfig.ReadOnly;
                  checkBoxGateway.Enabled = !FSelectedConfig.ReadOnly;
                  comboBoxService.Enabled = !FSelectedConfig.ReadOnly;
               }
            }
            finally
            {
               FAssigningConfig = false;
            }
         }
      }

      private void buttonCopy_Click(object sender, EventArgs e)
      {
         SelectedConfig = new XmppServiceConfig(SelectedConfig, true);
      }
      private void buttonSave_Click(object sender, EventArgs e)
      {
         if (!FSelectedConfig.ReadOnly)
         {
            FSelectedConfig.Host = textBoxHost.Text;
            FSelectedConfig.Name = comboBoxConfig.Text;
            FSelectedConfig.Port = ushort.Parse(textBoxPort.Text);
            FSelectedConfig.ViaGateway = checkBoxGateway.Checked;
            if (comboBoxService.SelectedIndex >= 0)
            {
               FSelectedConfig.Service = (ServiceInfo)comboBoxService.SelectedItem;
            }
         }
         FSelectedConfig.Resource = textBoxResource.Text;
         FSelectedConfig.Priority = (short)numericPriority.Value;
         FSelectedConfig.Security = (SecurityEnum)comboBoxSecurity.SelectedItem;
         FSelectedConfig.Authentication = (AuthenticationsEnum)comboBoxAuth.SelectedItem;

         IEnumerable<ServiceConfig> configurations = FServiceDriver.Configurations;

         if (!configurations.Contains(FSelectedConfig))
         {
            FServiceDriver.AddConfig(FSelectedConfig);
            comboBoxConfig.Items.Insert(0, FSelectedConfig);
         }
         foreach (var conf in configurations)
         {
            if (!comboBoxConfig.Items.Contains(conf)) FServiceDriver.RemoveConfig(conf);
         }

         FServiceDriver.FireSettingsChanged();

         Close();
      }
      private void buttonProxy_Click(object sender, EventArgs e)
      {
         FServiceDriver.Communicator.ShowSettings("Network");
      }
      private void buttonClose_Click(object sender, EventArgs e)
      {
         Close();
      }
      private void buttonDelete_Click(object sender, EventArgs e)
      {
         if (!FSelectedConfig.ReadOnly)
         {
            if (MessageBox.Show(Resources.S_Delete_config.Replace("<config>", FSelectedConfig.Name), Text, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
               comboBoxConfig.Items.Remove(SelectedConfig);
               
               if (comboBoxConfig.Items.Count > 0)
               {
                  comboBoxConfig.SelectedIndex = 0;
               }
               else
               {
                  SelectedConfig = null;
               }
            }
         }
      }
      private void buttonCreate_Click(object sender, EventArgs e)
      {
         XmppServiceConfig newConf = new XmppServiceConfig(FServiceDriver.DefaultConfig as XmppServiceConfig, true);

         newConf.Name = "New_Configuration";

         SelectedConfig = newConf;
      }
      private void checkBoxResource_CheckedChanged(object sender, EventArgs e)
      {
         if (checkBoxResource.Checked)
         {
            textBoxResource.Enabled = false;
            textBoxResource.Text = Environment.MachineName;
         }
         else
         {
            textBoxResource.Enabled = true;
         }
      }
      private void comboBoxConfig_SelectedIndexChanged(object sender, EventArgs e)
      {
         if (!FAssigningConfig) SelectedConfig = (XmppServiceConfig)comboBoxConfig.SelectedItem;
      }
      private void comboBoxService_SelectedIndexChanged(object sender, EventArgs e)
      {
         if (!FAssigningConfig && comboBoxService.SelectedItem is ServiceInfo)
         {
            checkBoxGateway.Checked = ((ServiceInfo)comboBoxService.SelectedItem).ProtocolName != "XMPP";
         }
      }
   }
}